<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 学生基本信息表
 *
 * @author wutian
 */

namespace Home\Model ;

class StudentBaseModel extends \Think\Model
{

    /**
     * 获取学生基本信息
     * @param type $UID
     * @return boolean
     */
    function getStudentBaseInforByUID ( $UID )
    {
        if ( ! is_numeric ( $UID ) )
        {
            return false ;
        }
        $rs = $this->where ( 'UID = %s' , $UID )->find () ;
        if ( is_array ( $rs ) )
        {
            return $rs ;
        }
        return false ;
    }

    /**
     * 录入学生基本信息
     * @param type $UID
     * @param type $UName
     * @param type $UBirthday
     * @param type $UIDCardNumber
     * @param type $UEmail
     * @param type $UPhoneNumber
     * @param type $UQQ
     * @param type $UPicturePath
     * @param type $AcademyID
     * @param type $ProfessionID
     * @param type $GradeID
     * @param type $ClassID
     * @return boolean
     */
    function addStudentBaseInfor ( $UID , $UName , $UBirthday , $UIDCardNumber ,
            $UEmail , $UPhoneNumber , $UQQ , $UPicturePath , $AcademyID ,
            $ProfessionID , $GradeID , $ClassID )
    {
        $map[ 'UID' ]           = $UID ;
        $map[ 'UName' ]         = $UName ;
        $map[ 'UBirthday' ]     = $UBirthday ;
        $map[ 'UIDCardNumber' ] = $UIDCardNumber ;
        $map[ 'UEmail' ]        = $UEmail ;
        $map[ 'UPhoneNumber' ]  = $UPhoneNumber ;
        $map[ 'UQQ' ]           = $UQQ ;
        $map[ 'UPicturePath' ]  = $UPicturePath ;
        $map[ 'AcademyID' ]     = $AcademyID ;
        $map[ 'ProfessionID' ]  = $ProfessionID ;
        $map[ 'GradeID' ]       = $GradeID ;
        $map[ 'ClassID' ]       = $ClassID ;
        if ( $this->create ( $map ) )
        {
            if ( ! $this->add () )
            {
                return false ;
            }
            return true ;
        }
        return false ;
    }

    /**
     * 修改学生基本信息
     * @param type $UID
     * @param type $UName
     * @param type $UBirthday
     * @param type $UIDCardNumber
     * @param type $UEmail
     * @param type $UPhoneNumber
     * @param type $UQQ
     * @param type $UPicturePath
     * @param type $AcademyID
     * @param type $ProfessionID
     * @param type $GradeID
     * @param type $ClassID
     * @return boolean
     */
    function updateStudentBaseInfor ( $UID , $UName , $USex , $UIDCardNumber ,
            $UEmail , $UPhoneNumber , $UQQ , $UPicturePath , $AcademyID ,
            $ProfessionID , $GradeID , $ClassID , $UBirthday )
    {
        $map[ 'UBirthday' ]     = $UBirthday ;
        $map[ 'USex' ]          = $USex ;
        $map[ 'UName' ]         = $UName ;
        $map[ 'UIDCardNumber' ] = $UIDCardNumber ;
        $map[ 'UEmail' ]        = $UEmail ;
        $map[ 'UPhoneNumber' ]  = $UPhoneNumber ;
        $map[ 'UQQ' ]           = $UQQ ;
        $map[ 'UPicturePath' ]  = $UPicturePath ;
        $map[ 'AcademyID' ]     = $AcademyID ;
        $map[ 'ProfessionID' ]  = $ProfessionID ;
        $map[ 'GradeID' ]       = $GradeID ;
        $map[ 'ClassID' ]       = $ClassID ;
        $rs                     = $this->where ( 'UID = %s' , $UID )->save ( $map ) ;
        if ( false === $rs )
        {
            return false ;
        }
        return true ;
    }

    //验证邮箱格式是否正确
    function checkEmailFormat ( $UEmail )
    {
        if ( ! eregi ( "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})" ,
                        $UEmail ) )
        {
            return false ;
        }
        return true ;
    }

    //验证手机号码格式是否正确
    function checkTelFormat ( $UPhoneNumber )
    {
        if ( ! preg_match ( "/^13[0-9]{1}[0-9]{8}$|15[012356789]{1}[0-9]{8}$|18[012356789]{1}[0-9]{8}$|14[57]{1}[0-9]$/" ,
                        $UPhoneNumber ) )
        {
            return false ;
        }
        return true ;
    }

    /**
     * 验证身份证号码是否有效
     * @param string $UIdcardNumber 身份证号码
     * @return boolean
     * true 身份证号码有效
     * false 身份证号码无效
     */
    function checkUIDCardnumber ( $UIdcardNumber )
    {
        $City = array ( 11 => "北京" , 12 => "天津" , 13 => "河北" , 14 => "山西" , 15 => "内蒙古" ,
            21 => "辽宁" , 22 => "吉林" , 23 => "黑龙江" , 31 => "上海" , 32 => "江苏" , 33 => "浙江" ,
            34 => "安徽" , 35 => "福建" , 36 => "江西" , 37 => "山东" , 41 => "河南" , 42 => "湖北" ,
            43 => "湖南" , 44 => "广东" , 45 => "广西" , 46 => "海南" , 50 => "重庆" , 51 => "四川" ,
            52 => "贵州" , 53 => "云南" , 54 => "西藏" , 61 => "陕西" , 62 => "甘肃" , 63 => "青海" ,
            64 => "宁夏" , 65 => "新疆" , 71 => "台湾" , 81 => "香港" , 82 => "澳门" , 91 => "国外" ) ;

        //长度验证
        $length = strlen ( $UIdcardNumber ) ;
        if ( $length != 18 )
        {

            return false ;
        }

        //地区验证
        if ( ! array_key_exists ( intval ( substr ( $UIdcardNumber , 0 , 2 ) ) ,
                        $City ) )
        {

            return false ;
        }

        //18位身份证处理
        $sBirthday = substr ( $UIdcardNumber , 6 , 4 ) . '-' . substr ( $UIdcardNumber ,
                        10 , 2 ) . '-' . substr ( $UIdcardNumber , 12 , 2 ) ;

        //验证日期格式
        $unixTime = strtotime ( $sBirthday ) ;
        if ( ! $unixTime )
        {

            return false ;
        }
        //校验日期是否有效
        if ( date ( "Y-m-d" , $unixTime ) != $sBirthday )
        {

            return false ;
        }

        //身份证编码规范验证
        $idcard_base = substr ( $UIdcardNumber , 0 , 17 ) ;
        if ( strtoupper ( substr ( $UIdcardNumber , 17 , 1 ) ) != $this->getVerifyBit ( $idcard_base ) )
        {

            return false ;
        }
        return true ;
    }

    /**
     * 计算身份证校验码，根据国家标准GB 11643-1999
     * @param string $idcard_base 身份证前17位
     * @return string|boolean 返回身份证第18位
     */
    function getVerifyBit ( $idcard_base )
    {
        if ( strlen ( $idcard_base ) != 17 )
        {

            return false ;
        }
        //加权因子
        $factor             = array ( 7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , 6 , 3 , 7 ,
            9 , 10 , 5 , 8 , 4 , 2 ) ;
        //校验码对应值
        $verify_number_list = array ( '1' , '0' , 'X' , '9' , '8' , '7' , '6' , '5' ,
            '4' , '3' , '2' ) ;
        $checksum           = 0 ;
        for ( $i = 0 ;
                $i < strlen ( $idcard_base ) ;
                $i ++ )
        {
            $checksum += substr ( $idcard_base , $i , 1 ) * $factor[ $i ] ;
        }
        $mod           = $checksum % 11 ;
        $verify_number = $verify_number_list[ $mod ] ;
        return $verify_number ;
    }

}
